package com.glorysoft.sunnypda.ui.downloadrecipe

import android.os.Build
import android.os.Bundle
import android.view.View
import androidx.annotation.RequiresApi
import com.glorysoft.sunnypda.BR
import com.glorysoft.sunnypda.R
import com.glorysoft.sunnypda.base.BaseActivity
import com.glorysoft.sunnypda.databinding.ActivityDownloadRecipeBinding
import com.glorysoft.sunnypda.entity.EqpInfoEntity
import com.glorysoft.sunnypda.entity.ui.ToastMessageBean
import com.glorysoft.sunnypda.utils.PopWindowsUtils
import kotlinx.android.synthetic.main.activity_download_recipe.*

/**
 * FileName: DownLoadRecipeActivity
 * Author: XJH
 * Date: 2022/1/11 16:34
 * Description:
 * History:
 */
class DownLoadRecipeActivity : BaseActivity<DownLoadRecipeViewModel,ActivityDownloadRecipeBinding>(),
    View.OnClickListener {
    private var recipePop: PopWindowsUtils.GeneralPop? = null

    override fun layoutId(): Int {
        return R.layout.activity_download_recipe
    }

    override fun initVariableId(): Int {
        return BR.downLoadRecipeViewModel
    }

    override fun initView(savedInstanceState: Bundle?) {
        setTitle(getString(R.string.download_recipe))

        initEdit(etEqpWork,object : EditTextListener{
            override fun onTextChangeListener(s: String) {
                viewModel.getRecipeList(s)
            }

        })

        flRecipeList.setOnClickListener(this)
        tvDeleteList.setOnClickListener(this)
    }

    override fun initData() {
//        setRightListener(getString(R.string.download)) {
//
//        }
        viewModel.defUI.callObserve.observe(this){
            when(it.type){
                0 ->{
                    viewModel.defUI.toastEvent.value =
                        ToastMessageBean(getString(R.string.success), true)
                }
                1 ->{
                    viewModel.defUI.toastEvent.value =
                        ToastMessageBean(getString(R.string.invalid), false)
                }
            }

        }
    }

    override fun onClick(v: View?) {
        when(v?.id){
            R.id.flRecipeList ->{
                if (etEqpWork.text.toString().isEmpty()){
                    viewModel.defUI.toastEvent.value = ToastMessageBean(getString(R.string.eqp_hint),false)
                    return
                }
                if (viewModel.mRecipeList.size == 0){
                    viewModel.defUI.toastEvent.value = ToastMessageBean(getString(R.string.return_area_is_empty),false)
                    return
                }
                if (recipePop == null) {
                    recipePop = PopWindowsUtils.getOneChoosePop(
                        this,
                        flRecipeList.width,
                        viewModel.mRecipeList.toSortedSet().toList(),
                        ivRecipeArrow,
                        object :
                            PopWindowsUtils.PopTextAndPositionCallback {
                            @RequiresApi(Build.VERSION_CODES.P)
                            override fun onSuccess(codeS: String, potion: Int) {
                                tvRecipe.text = codeS
                                viewModel.getRecipeInfoByRecipe(codeS)
                            }

                        })

                }

                if (recipePop!!.isShowing) {
                    recipePop?.dismiss()
                } else {
                    PopWindowsUtils.setArrowShow(ivRecipeArrow)
                    recipePop?.showAsDropDown(flRecipeList, 0, 0)
                }
            }
            R.id.tvDeleteList ->{
                viewModel.onDeleteList()
            }
        }
    }
}